package com.psa.system;

import com.psa.element.IJson;
import com.psa.element.RequestCmd;
import com.psa.element.RequestMessage;
import com.psa.tool.IHttpCallback;
import com.psa.tool.SystemStatus;
import com.wxw.BaseLibrary.element.JsonObject;

/**
 * @author david
 */
public class HttpBaseActivity extends BaseActivity implements IHttpCallback {
    protected RequestCmd currentCmd = null;
    protected String errorMsg = "";
    protected int state = -1;

    protected Runnable showErrorCallback = new Runnable() {
        @Override
        public void run() {
            if (waitingDialog != null) {
                waitingDialog.stop();
            }
            SystemStatus.showMessage(HttpBaseActivity.this, errorMsg);
        }
    };

    protected Runnable updateCallback = new Runnable() {
        @Override
        public void run() {
            successCallback();
        }
    };

    protected void successCallback() {
        String msg = "";
        JsonObject jsonObject = new JsonObject(currentCmd.Result);
        if (currentCmd.Result.equals("")) {
            msg = "网络数据错误！";
        } else {
            msg = jsonObject.getProperty("msg").getValue();
            state = jsonObject.getProperty("status").getInteger();
        }
        SystemStatus.showMessage(this, msg);
        if (waitingDialog != null) {
            waitingDialog.stop();
        }
    }

    @Override
    public void callCompleted(RequestMessage message, IJson requestCmd) {
        currentCmd = (RequestCmd)requestCmd;
        runOnUiThread(updateCallback);
    }

    @Override
    public void callError(RequestMessage message, String err) {
        currentCmd = null;
        errorMsg = "Error message:" + err;
        runOnUiThread(showErrorCallback);
    }
}
